
package com.xxxx.service;

import com.xxxx.entity.Blog;
import com.xxxx.entity.vo.MessageModel;
import com.xxxx.mapper.BlogMapper;
import com.xxxx.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BlogService {
    public MessageModel createBlog(Blog blog) {
        MessageModel mm = new MessageModel();
        try (SqlSession sqlSession = GetSqlSession.createSqlSession()) {
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            mapper.insertBlog(blog);
            sqlSession.commit();
            mm.setCode(1);
            mm.setMsg("博客发布成功");
        } catch (Exception e) {
            mm.setCode(0);
            mm.setMsg("系统错误：" + e.getMessage());
        }
        return mm;
    }

    public List<Blog> getUserBlogs(Integer userId) {
        try (SqlSession sqlSession = GetSqlSession.createSqlSession()) {
            return sqlSession.getMapper(BlogMapper.class).selectBlogsByUserId(userId);
        }
    }
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======

>>>>>>> 2c9a92f (新增删除博客功能)
=======

>>>>>>> a05f6f6 (密码密文存储)
=======

>>>>>>> 2acfcf7 (详情页)
=======

>>>>>>> d93a5bb (移除index页)
    public List<Blog> getAllBlogs() {
        try (SqlSession sqlSession = GetSqlSession.createSqlSession()) {
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            return mapper.selectAllBlogs(); // 调用Mapper层方法
        }
    }
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
}
=======
=======
>>>>>>> a05f6f6 (密码密文存储)
=======
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)

    public boolean deleteBlog(int blogId, int currentUserId) {
        try (SqlSession sqlSession = GetSqlSession.createSqlSession()) {
            BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);


            int result = blogMapper.deleteBlog(blogId, currentUserId);
            sqlSession.commit();


            return result > 0;
        }
    }
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
}


>>>>>>> 2c9a92f (新增删除博客功能)
=======

    public Blog getBlogById(int blogId) throws Exception {
        try (SqlSession session = GetSqlSession.createSqlSession()) {
            BlogMapper mapper = session.getMapper(BlogMapper.class);
            return mapper.getBlogById(blogId);
        }
    }

}


>>>>>>> a05f6f6 (密码密文存储)
=======
=======
>>>>>>> d93a5bb (移除index页)

    public Blog getBlogById(int blogId) {
        try (SqlSession session = GetSqlSession.createSqlSession()) {
            BlogMapper mapper = session.getMapper(BlogMapper.class);
            return mapper.getBlogById(blogId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}


<<<<<<< HEAD
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
